import asyncio
import logging

from cjc import project_setting
from cjc.tools.logger import Logger
from cjc.tools.memory_share_helper import MemoryShareHelper
from cjc.tools.memory_share_helper_ubuntu import MemoryShareUbuntuHelper
from cjc.webapi.web_api import DjangoWebApi
from cjc.ws_server.data_cache import put_message_to_queue
from cjc.ws_server.tools import create_backend_cmd_msg


class ClientMsgHandler:
    front_client_msg_code = 600  # 标记前端消息
    front_client_cmd_code = 601  # 标记前端消息
    detect_client_msg_code = 700  # 标记检测器消息
    detect_client_cmd_code = 701  # 标记检测器消息
    ws_server_cmd_code = 800  # 标记检测器消息
    app_start_track = 1001
    app_stop_track = 1000
    memory_helper = MemoryShareUbuntuHelper()
    web_api = DjangoWebApi()
    project_id = project_setting.project_id
    last_file_path = None
    cover_content = None


    def __init__(self, can_track):
        self.web_api.login('superadmin', "admin123456")
        self.can_track = can_track
        self.log = Logger(__class__.__name__)

    async def handle(self, recv_dic):
        await  asyncio.sleep(0)
        if recv_dic.code == self.app_start_track:  # 开始track
            self.can_track.value = 1
            self.log.info('开始发送', self.can_track.value)
        elif recv_dic.code == self.app_stop_track:  # 停止track
            self.can_track.value = 0
            await put_message_to_queue(create_backend_cmd_msg(0))
            self.log.info('停止发送', self.can_track.value)
        elif recv_dic.code == self.front_client_cmd_code:  # 请求图片
            pass

    async def upload_video_record(self, msg):

        self.web_api.create_or_update_video_clip(proj_id=msg.proj_id, _id=None,
                                                 src_name=msg.src_name,
                                                 cover_name=msg.cover_name,
                                                 cover_content= msg.cover_content,
                                                 src_content=msg.src_content,
                                                 track=msg.track
                                                 )
        await asyncio.sleep(0)


